﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TFSAccess;

namespace CreateConnectedTask
{
    internal class WorkItemBase : IWorkItem
    {
        List<string> hash = new List<string>();
        private readonly TFSAccess _TFS = null;
        private IList<WorkItem> _ListWorkItem = null;
        public WorkItemBase(TFSAccess tfs)
        {
            _TFS = tfs;
            hash.Add("AN");
            hash.Add("AR");
            hash.Add("CUT");
            hash.Add("CoR");
            hash.Add("UTCW");
            hash.Add("UTCR");
        }
        public void Create()
        {


        }

        public void Create(string[] wi, ref string errorMessage)
        {
            WorkItem _WorkItem;
            WorkItemTypeCollection wiType = _TFS.WorkItemStore.Projects[_TFS.SelectedProject()].WorkItemTypes;
            IList<WorkItem> validUserStory = ValidateWorkItem(wi, ref errorMessage);
            if (validUserStory.Count > 0)
            {
                _ListWorkItem = new List<WorkItem>();
            }
            foreach (var userStoryWI in validUserStory)
            {
                for (int i = 0; i < 6; i++)
                {
                    _WorkItem = null;
                    _WorkItem = wiType["Task"].NewWorkItem();
                    string title = userStoryWI.Id.ToString().Trim() + "-" + userStoryWI.Title + "-" + hash[i];
                    DesignWorkItem(_WorkItem, title);
                    ArrayList errors = _WorkItem.Validate();
                    if (errors != null && errors.Count > 0)
                    {
                        foreach (var errorItem in errors)
                        {
                            errorMessage += "In user story:" + userStoryWI + " : " + errorItem.ToString() + Environment.NewLine;
                        }
                    }
                    else
                    {
                        _WorkItem.Save();
                        LinkToWorkItem(userStoryWI, _WorkItem.Id);
                        userStoryWI.Save();
                    }
                }
            }
        }
        public void UpdateField(string fieldName, object value)
        {
            throw new NotImplementedException();
        }

        public object GetFieldValue(string fieldName)
        {
            throw new NotImplementedException();
        }

        public IList<WorkItem> ValidateWorkItem(string[] wi, ref string errorMessage)
        {
            IList<WorkItem> validUserStory = null;
            string query = string.Empty;
            errorMessage = string.Empty;

            query = "SELECT [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State] FROM WorkItems ";
            WorkItemCollection queryResults = _TFS.WorkItemStore.Query(query);
            //_TFS.WorkItemStore.GetWorkItem (
            if (queryResults.Count > 0)
            {
                validUserStory = new List<WorkItem>();
                foreach (WorkItem workItem in queryResults)
                {
                    var res = (from x in wi where Convert.ToInt32(x.Trim()) == workItem.Id select x).FirstOrDefault();
                    if (!string.IsNullOrWhiteSpace(res))
                    {
                        validUserStory.Add(workItem);
                    }
                }
            }
            return validUserStory;
        }

        private void DesignWorkItem(WorkItem wi, string title)
        {
            wi.Title = title;
            wi.Fields[Constants.Task_AssignedTo].Value = "ABC";
        }
        private void LinkToWorkItem(WorkItem userStoryWI, int id)
        {
            Link link = new RelatedLink(id);
            link.Comment = "Related link of Development Task with UserStory";
            userStoryWI.Links.Add(link);
        }
    }
}
